/*
 * @Description: 融云消息测试
 * @Author: GGB
 * @Date: 2022-01-05 14:28:52
 * @LastEditors: GGB
 * @LastEditTime: 2022-02-22 10:40:02
 */
import * as RongIMLib from '@rongcloud/imlib-next';
import { notification } from 'ant-design-vue';

// 初始化融云
const environment = process.env.NODE_ENV !== 'production';
console.info('是开发环境吗？', environment);
console.info('当前环境', process.env.NODE_ENV);
if (environment) {
  RongIMLib.init({ appkey: 'z3v5yqkbzi0g0' });
} else {
  RongIMLib.init({ appkey: 'z3v5yqkbzi0g0' });
}
// 定义融云的事件
const Events = RongIMLib.Events;
/**
 * 正在链接的事件状态
 */
RongIMLib.addEventListener(Events.CONNECTING, () => {
  console.log('正在链接...');
});

/**
 * 链接到服务器会触发这个事件
 */
RongIMLib.addEventListener(Events.CONNECTED, () => {
  console.log('连接成功');
});

/**
 * 手动调用 disconnect 方法或者用户被踢下线 会触发这个事件
 */
RongIMLib.addEventListener(Events.DISCONNECT, async () => {
  console.log('连接中断，需要业务层进行重连处理');
});

/**
 * 链接出问题时，内部进行重新链接，会出发这个事件
 */
RongIMLib.addEventListener(Events.SUSPEND, () => {
  console.log('链接中断，SDK 会尝试重连，业务层无需关心');
});
/**
 * 监听消息放松
 */
RongIMLib.addEventListener(Events.MESSAGES, (res) => {
  const messages = res.messages;
  const now = Date.parse(new Date().toString()) / 1000;
  if (messages?.length) {
    messages.forEach((message) => {
      const type = message.messageType;
      const sendTimestamp = message.content.data
        ? message.content.data.timestamp
        : message.content.timestamp;
      const content = message.content.data ? message.content.data.content : message.content.notice;
      if (now - sendTimestamp <= 600) {
        messageTypeList.forEach((messageType) => {
          if (messageType.type === type && messageType.audio) {
            // 播放音频
            let audio = new Audio();
            audio.src = messageType.audio;
            audio.play();
            // 方式二
            // const audio = document.createElement('audio');
            // // require一个本地文件，会变成base64格式
            // audio.src = messageType.audio;
            // document.body.appendChild(audio);
            // // play返回的是一个promise
            // audio.play();
          }
        });
        setTimeout(() => {
          notification.success({
            message: '温馨提示',
            description: content,
            duration: 10,
          });
        }, 500);
      }
    });
  }
});
export const RongIMLibInit = async (access_token) => {
  if (!access_token) return;
  const linkInfo = await RongIMLib.connect(access_token);
  if (linkInfo.code !== 0)
    notification.error({
      message: '温馨提示',
      description: '融云链接失败',
    });
};

interface messageTypeListModel {
  id: number;
  message: string;
  type: string;
  title: string;
  description?: string;
  audio?: string;
}
// 获取融云的消息
const messageTypeList: messageTypeListModel[] = [
  {
    id: 1,
    message: '你有一个新的外卖订单,请处理',
    type: 'APP:NOTICENEWORDER',
    title: '外卖订单',
    description: '新订单',
    audio: 'https://delivery.yrdspbd.com/images/10001.mp3',
  },
  {
    id: 2,
    message: '有订单申请取消，请处理',
    type: 'APP:NOTICECANCELORDER',
    title: '外卖订单',
    description: '新订单',
  },
  {
    id: 3,
    message: '有订单申请取消，请处理',
    type: 'LAYIM:BOX',
    title: '外卖订单',
    description: ' 有订单申请取消，请处理',
    audio: 'https://delivery.yrdspbd.com/images/10005.mp3',
  },
  {
    id: 4,
    message: '有订单申请取消，请处理',
    type: 'APP:NOTICEAFTERSALEORDER',
    title: '外卖订单',
    description: '有订单申请售后，请处理！',
  },
];



/**
 * 关闭连接
 */
export const RongIMLibClose = () => {
  RongIMLib.disconnect();
};
